﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace Comarch.Animation.SL5Example
{
    public partial class MainPage
    {
        private readonly DataLayer _dataLayer = new DataLayer();
        public bool IsReloadAvailable { get; set; }
        public int ActualSelectedItem { get; set; }

        public MainPage()
        {
            InitializeComponent();
            VisualStateManager.GoToState(this, "GroupElements", false);
            LayoutRoot.DataContext = _dataLayer;
            radioPanel.IsChecked = true;
        }

        private void EffectListBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            IsReloadAvailable = true;
            var item = (SingleAnimation)((System.Windows.Controls.Primitives.Selector)(sender)).SelectedItem;
            if (item == null) return;
            ActualSelectedItem = ((System.Windows.Controls.Primitives.Selector) (sender)).SelectedIndex;
            Animate();
        }
         
        private void UpdateDescription(SingleAnimation item)
        {
            var copyEfect = item.AnimationDescription;
            if (radioSingle.IsChecked == true)
            {
                description1.Text =
                    String.Format(
                        "new Storyboard().AddStoryboard({0}, Animation, border).Begin(); \n\rprivate void Animation(FrameworkElement element, StoryboardBuilder sbb)\n{{\n\t{1}\n}};",
                        _dataLayer.InitialOffset, copyEfect);
                return;
            }
            description1.Text =
                String.Format(
                    "new Storyboard().AddStoryboardsForObjects({0}, {1}, Animation, border).Begin(); \n\rprivate void Animation(FrameworkElement element, StoryboardBuilder sbb)\n{{\n\t{2}\n}};",
                    _dataLayer.InitialOffset,_dataLayer.DelayBetweenElements, copyEfect);
        }

        private void FreezeUI()
        {
            effectsListBox.IsEnabled = false;
            reloadButton.IsEnabled = false;
        }

        private void UnFreezeUI()
        {
            effectsListBox.IsEnabled = true;
            reloadButton.IsEnabled = true;
        }

        private void Animate()
        {
            FreezeUI();
            var item1 = (SingleAnimation)effectsListBox.Items[ActualSelectedItem];
            if (radioSingle.IsChecked.Value)
            {
                new Storyboard().AddStoryboard(_dataLayer.InitialOffset, item1.Animation, border).OnCompletedDo(UnFreezeUI).Begin();
            }
            else if (radioPanel.IsChecked.Value)
            {
                new Storyboard().AddStoryboardsForObjects(_dataLayer.InitialOffset, _dataLayer.DelayBetweenElements, item1.Animation, stackPanel).OnCompletedDo(UnFreezeUI).Begin();
            }
            else
            {
                new Storyboard().AddStoryboardsForObjects(_dataLayer.InitialOffset, _dataLayer.DelayBetweenElements, item1.Animation, MakeElementsList()).OnCompletedDo(UnFreezeUI).Begin();
            }
            UpdateDescription(item1);
        }

        private IEnumerable<FrameworkElement> MakeElementsList()
        {
            var elements = new List<FrameworkElement> { border1, border2, border3, border4, border5, border6 };
            return elements;
        }

        private void TypeAnimationClick(object sender, RoutedEventArgs e)
        {
            description1.Text = String.Empty;
            var item = ((RadioButton)sender).Name;
            if (item == "radioSingle")
            {
                VisualStateManager.GoToState(this, "SingleElement", false);
                return;
            }
            if (item == "radioPanel")
            {
                VisualStateManager.GoToState(this, "GroupElements", false);
                return;
            }
            VisualStateManager.GoToState(this, "RandomElements", false);
        }

        private void ReloadClicked(object sender, RoutedEventArgs e)
        {
            if (!IsReloadAvailable) return;
            Animate();
        }
    }
}
